EmSAT Achieve Exam  >  EmSAT Achieve Questions  >  What will be the output of the following code... Start Learning for Free
What will be the output of the following code?
int[] array = {5, 10, 15, 20};
int target = 15;
int index = -1;
for (int i = 0; i < array.length; i++) {
    if (array[i] == target) {
        index = i;
        break;
    }
}
System.out.println(index);
  • a)
    2
  • b)
    3
  • c)
    -1
  • d)
    Compiler error
Correct answer is option 'A'. Can you explain this answer?
Most Upvoted Answer
What will be the output of the following code?int[] array = {5, 10, 15...
Explanation:

Initialization:
- An integer array named `array` is initialized with values {5, 10, 15, 20}.
- An integer variable named `target` is initialized with value 15.
- An integer variable named `index` is initialized with -1.

Loop and Condition:
- A for loop iterates over the elements of the array.
- Inside the loop, it checks if the current element in the array is equal to the target value.
- If the condition is true, it assigns the index of the element to the `index` variable and breaks out of the loop using the `break` statement.

Output:
- In the given array, the element with value 15 is present at index 2 (0-based indexing).
- So, the value of `index` variable becomes 2.
- The output of the code will be 2 when `System.out.println(index);` is executed.
Therefore, the correct output of the code is 2.
Free Test
Community Answer
What will be the output of the following code?int[] array = {5, 10, 15...
The code searches for the index of the 'target' value in the 'array'. It starts with an initial index of -1 and iterates through the array, checking if each element is equal to the target value. If a match is found, the index is updated and the loop breaks. The final index is printed, which is 2.
Attention EmSAT Achieve Students!
To make sure you are not studying endlessly, EduRev has designed EmSAT Achieve study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in EmSAT Achieve.
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer?
Question Description
What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer? for EmSAT Achieve 2024 is part of EmSAT Achieve preparation. The Question and answers have been prepared according to the EmSAT Achieve exam syllabus. Information about What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer? covers all topics & solutions for EmSAT Achieve 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer?.
Solutions for What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for EmSAT Achieve. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.
Here you can find the meaning of What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer?, a detailed solution for What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer? has been provided alongside types of What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following code?int[] array = {5, 10, 15, 20};int target = 15;int index = -1;for (int i = 0; i < array.length; i++) { if (array[i] == target) { index = i; break; }}System.out.println(index);a)2b)3c)-1d)Compiler errorCorrect answer is option 'A'. Can you explain this answer? tests, examples and also practice EmSAT Achieve tests.
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Explore Courses

Suggested Free Tests

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev